import {
  getHeadquarters,
  getRegion,
  getCity,
  getWarehouse,
  getCustomerName,
  getLogisticsList,
  getOwnerName
} from '@/api/api'
import moment from 'moment'
import PageContainer from '@/components/layouts/PageContainer.vue'
import Operation from '@/components/base/operation.vue'
import operationItem from '@/components/base/operation-item.vue'
import SearchGroupBtn from '@/components/base/searchGroupBtn.vue'
import AddBtn from '@/components/base/addBtn.vue'
import SelectNum from '@/components/base/selectNum.vue'
import DelModal from '@/components/base/delModal.vue'
import BottomPagination from '../views/meter/BottomPagination'
import ExpressModal from '@/components/base/expressModal'
import WarringModal from '@/components/base/delModal'
import { downLoadTemp } from '@/utils/util'
const delMixin = {
  data() {
    return {
      placement: 'right', //抽屉打开方式
      precision: 2, //保留几位小数
      isOpen: true, //是否展开
      tableData: [], //表格数据
      tableLoading: false, //表格加载状态
      pageNo: 1, //当前页码
      pageSize: 10, //每页条数
      total: 0, //总条数
      id: '', //单条数据id
      editVisible: false, //删除框状态
      ids: '', //选中的id ,隔开
      selectedRowKeys: [], //选中集合
      selectNumber: 0, //选中条数
      deletdBtnShow: false, // 批量删除按钮是否显示
      // 公共数据筛选项目
      headquartersId: undefined, // 总部id
      regionId: undefined, // 大区id
      cityId: undefined, // 城市id
      organizeId: undefined, // 仓库id
      regionList: [], //大区集合
      cityList: [], //城市集合
      warehouseList: [], //仓库集合
      ownerList: [], //货主集合
      status: undefined, //状态
      customerCode: undefined, //客户编码
      ownerCode: undefined, //货主编码
      customerList: [], //客户列表
      componyCode: undefined, //快递编码
      logisticsList: [], //快递公司集合，
      createBy: '', // 创建人
      createTime: [], // 创建时间
      updateBy: '', // 修改人
      updateTime: [], // 修改时间
      projectName: undefined, //项目名称
      createTime_begin: '', // 创建时间开始时间
      createTime_end: '', // 创建时间结束时间
      updateTime_begin: '', // 修改时间开始时间
      updateTime_end: '', // 修改时间结束时间
      remark: '', //备注
      code: '', //编码
      dateFormat: 'YYYY-MM-DD HH:mm:ss', //日期格式
      // 抽屉相关
      drawerTitle: '新增', //抽屉标题
      labelCol: { span: 7 },
      wrapperCol: { span: 16 }
    }
  },
  components: {
    PageContainer,
    DelModal,
    BottomPagination,
    SelectNum,
    AddBtn,
    SearchGroupBtn,
    Operation,
    operationItem,
    ExpressModal,
    WarringModal
  },
  created() {
    this.getHeadquartersList()
  },
  computed: {
    isDisabled() {
      if (this.selectedRowKeys.length === 0) return false
      return this.tableData.filter(item => this.selectedRowKeys.includes(item.id)).some(this.isListOneDisabled)
    }
  },
  methods: {
    moment,
    downLoadTemp,
    setBtn(record, key) {
      // 获取当前时间戳
      var timestamp1 = new Date().getTime() //当前时间戳
      var timestamp2 = new Date(record[key]).getTime()

      if (timestamp1 > timestamp2) {
        // 当前时间大于开始事件就禁用
        return true
      } else {
        return false
      }
    },
    // 获取总部
    getHeadquartersList() {
      let params = {}
      getHeadquarters(params).then(res => {
        if (res.success) {
          this.headquartersId = res.result.organizeOrgList[0].id
          this.getRegionList()
        }
      })
    },
    isListOneDisabled(item) {
      for (let i = 0; i < item.ruleDetailInfo.length; i++) {
        if (this.isOneDisabled(item.ruleDetailInfo[i])) {
          return true
        }
      }
    },
    isOneDisabled(item) {
      return this.setBtn(item, 'timeBegin')
    },
    // 是否存在一条处于有效期
    isOneTake(list) {
      return !list.some(this.isOneDisabled)
    },
    // 获取大区
    getRegionList() {
      let params = { id: this.headquartersId }
      getRegion(params).then(res => {
        if (res.success) {
          this.regionList = res.result.organizeOrgList
          if (this.regionList.length === 1) {
            this.regionId = this.regionList[0].id
            this.getCityList()
          }
        } else {
          this.$message.warning(res.message, 6)
        }
      })
    },
    // 选择大区
    regionChange(value) {
      this.regionId = value
      this.cityId = undefined
      this.organizeId = undefined
      this.cityList = []
      this.warehouseList = []
      this.getCityList()
      this.customerCode = undefined
      this.customerList = []
    },
    // 获取城市
    getCityList() {
      let params = {
        areaIds: this.regionId
      }
      getCity(params).then(res => {
        if (res.success) {
          this.cityList = res.result.organizeOrgList
          if (this.cityList.length === 1) {
            this.cityId = this.cityList[0].id
            this.getWarehouseList()
          }
        }
      })
    },
    // 选择城市
    cityChange(value) {
      this.cityId = value
      this.organizeId = undefined
      this.warehouseList = []
      this.getWarehouseList()
      this.customerCode = undefined
      this.customerList = []
    },
    // 获取仓库
    getWarehouseList() {
      let params = {
        cityIds: this.cityId
      }
      getWarehouse(params).then(res => {
        if (res.success) {
          this.warehouseList = res.result.organizeOrgList
          if (this.warehouseList.length === 1) {
            this.organizeId = this.warehouseList[0].id
            this.code = this.warehouseList[0].code
            // this.onSearch()
            this.getCustomerName()
          }
        }
      })
    },
    // 选择仓库
    warehouseChange(value) {
      this.organizeId = value
      for (var item of this.warehouseList) {
        if (item.id === this.organizeId) {
          this.code = item.code
          this.getCustomerName()
          this.customerCode = undefined
          this.customerList = []
        }
      }
    },
    // 获取客户名称列表
    getCustomerName() {
      let params = {
        whCode: this.code
      }
      getCustomerName(params).then(res => {
        if (res.success) {
          this.customerList = res.result
          // console.log(res.result);
        } else {
          this.$message.warning(res.message, 6)
        }
      })
    },
    // 选择客户
    customerChange() {
      this.ownerCode = undefined
      this.platformCode = undefined
      if (this.customerCode) {
        this.getOwnerName()
        this.getLogisticsList()
      }
    },
    // 获取快递公司
    getLogisticsList() {
      let params = {
        whCode: this.code,
        customerCode: this.customerCode
      }
      getLogisticsList(params).then(res => {
        if (res.success) {
          this.logisticsList = res.result.records
        }
      })
    },
    // 选择快递公司
    companyChange(value) {
      console.log(value)
    },
    isOpenChange(val) {
      this.isOpen = val
    },
    // 获取货主名称列表
    getOwnerName() {
      let params = {
        whCode: this.code,
        customerCode: this.customerCode
      }
      getOwnerName(params).then(res => {
        if (res.success) {
          this.ownerList = res.result
        } else {
          this.$message.warning(res.message, 6)
        }
      })
    },
    // 选择货主
    handleChange(value) {
      console.log(`selected ${value}`)
    },
    // 选择框过滤
    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
    onSearchCallback(type, callback,defaultVal='时必须先选择大区、城市和仓库！') {
      if (
        this.regionId == '' ||
        this.regionId == undefined ||
        this.cityId == '' ||
        this.cityId == undefined ||
        this.organizeId == '' ||
        this.organizeId == undefined
      ) {
        this.$message.warning(`${type}${defaultVal}`)
        return
      }
      this.pageNo = 1
      callback()
    },
    // 清空选中
    clearSelect() {
      this.selectNumber = 0
      this.selectedRowKeys = []
      this.deletdBtnShow = false
    },
    // 重置
    onReset() {
      this.regionId = undefined
      this.cityId = undefined
      this.organizeId = undefined
      this.projectName = null
      this.customerCode = undefined
      this.ownerCode = undefined
      this.componyCode = undefined
      this.status = undefined
      this.createBy = undefined
      this.updateBy = undefined
      this.createTime = undefined
      this.updateTime = undefined

      this.pageSize = 10
      this.pageNo = 1
      this.total = 0
      this.tableData = []
      this.selectNumber = 0
      this.selectedRowKeys = []
      this.deletdBtnShow = false
    },
    // 关闭抽屉并重置表单
    cancelForm() {
      this.visible = false
      this.$refs.ruleForm.resetFields()
    },
    onClose() {
      this.visible = false
    },
    // 选中
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
      this.selectNumber = selectedRowKeys.length
      if (this.selectedRowKeys.length > 0) {
        this.deletdBtnShow = true
      } else {
        this.deletdBtnShow = false
      }
    },
    // 批量删除
    batchDelete() {
      this.editVisible = true
      this.ids = this.selectedRowKeys.join(',')
    },
    // 确认删除对话框(单个删除)
    showModal(record) {
      this.editVisible = true
      this.ids = record.id
    },
    // 创建时间
    createChange(value, dateString) {
      this.createTime = value
      this.createTime_begin = dateString[0]
      this.createTime_end = dateString[1]
    },
    // 创建时间
    createOk(value) {
      console.log('onOk: ', value)
    },
    // 获取当前页数、条数
    pageChange(data1, data2) {
      this.pageNo = data1
      this.pageSize = data2
      this.getTableData()
    },
    // 获取当前页数、条数
    sizeChange(data1, data2) {
      this.pageNo = data1
      this.pageSize = data2
      this.getTableData()
    },
    // 修改时间
    editChange(value, dateString) {
      this.updateTime = value
      this.updateTime_begin = dateString[0]
      this.updateTime_end = dateString[1]
    },
    editOk(value) {
      console.log('onOk: ', value)
    },
    // 关闭弹窗
    handleNo() {
      this.editVisible = false
    },
    // 导出excel
    exportExcel() {
      this.ids = this.selectedRowKeys.join(',')
      this.exportExcelFun()
    },
    // ----------测试----------
    // 表单客户
    formCustomerChange() {
      this.form.ownerCode = undefined
      if (this.form.customerCode) {
        let params = {
          whCode: this.code,
          customerCode: this.form.customerCode
        }
        getOwnerName(params).then(res => {
          if (res.success) {
            this.form.ownerlist = res.result
          } else {
            this.$message.warning(res.message, 6)
          }
        })
      }
    }
  }
}

export { delMixin }
